package com.tmptSource.service.impl;

import com.tmpt.dto.Result;
import com.tmpt.utils.DateTool;
import com.tmpt.service.impl.DataLimitService;
import com.tmpt.utils.StringUtils;
import com.tmptSource.bean.*;
import com.tmptSource.dto.OwPictureMagDto;
import com.tmptSource.mapper.OwExecuteRecordMapper;
import com.tmptSource.mapper.OwIllegalRegisterMapper;
import com.tmptSource.dto.OwIllegalRegisterDto;
import com.tmptSource.dto.PageDto;
import com.tmptSource.mapper.OwPictureMagMapper;
import com.tmptSource.service.intf.IOwIllegalRegisterService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tmptSource.utils.dozer.MyMapper;
import com.tmptSource.utils.page.MyPageUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.dozer.Mapper;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by wxy on 2016/7/28.
 */
@Service(value="owIllegalRegisterService")
@Transactional(propagation = Propagation.REQUIRED)
public class OwIllegalRegisterService implements IOwIllegalRegisterService {

    private Logger log = Logger.getLogger(this.getClass());
    @Autowired private DataLimitService dataLimitService;
    @Autowired private Mapper mapper;
    @Autowired private MyMapper myMapper;
    @Autowired private MyPageUtils myPageUtils;
    @Autowired
    private OwIllegalRegisterMapper owIllegalRegisterMapper;
    @Autowired
    private OwPictureMagMapper owPictureMagMapper;
    @Autowired
    private OwExecuteRecordMapper owExecuteRecordMapper;



    @Override
    public Result<OwIllegalRegisterDto> pagination(OwIllegalRegisterDto owIllegalRegisterDto,PageDto pageDto) {
        //分页查询
        Result<OwIllegalRegisterDto> result = myPageUtils.pagination(
                owIllegalRegisterDto,pageDto,
                OwIllegalRegister.class,
                OwIllegalRegisterDto.class,
                owIllegalRegisterMapper,
                false,null);

        return result;
    }

    /**
    * 分布查询，有数据边界控制功能
    * @param owIllegalRegisterDto
    * @param pageDto
    * @param paramMap
    * @return
    */
    @Override
    public Result<OwIllegalRegisterDto> pagination(OwIllegalRegisterDto owIllegalRegisterDto,PageDto pageDto,Map<String,Object> paramMap) {
        //数据边界控制参数
        Map<String,Object> paramMap1 = dataLimitService.getLimitParam(paramMap);

        //分页查询
        Result<OwIllegalRegisterDto> result = myPageUtils.pagination(
                owIllegalRegisterDto,pageDto,
                OwIllegalRegister.class,
                OwIllegalRegisterDto.class,
                owIllegalRegisterMapper,
                paramMap1,
                false,null);

        return result;
    }


    @Override
    public Result<OwIllegalRegisterDto> selectAll() {
        Result<OwIllegalRegisterDto> result = new Result();

        //排序
        PageDto pageDto = new PageDto();
        pageDto.setSortColumn("id");
        pageDto.setSortOrder("desc");
        Map<String,Object> mapParam = new HashMap<>();
        mapParam.put("page",pageDto);

        List<OwIllegalRegister> list = owIllegalRegisterMapper.selectAll(mapParam);
        List<OwIllegalRegisterDto> dtoList = myMapper.map(list, OwIllegalRegisterDto.class);
        result.setResultList(dtoList);

        return result.success();
    }

    @Override
    public Result<OwIllegalRegisterDto> selectById(long id) {
        Result<OwIllegalRegisterDto> result = new Result();

        OwIllegalRegister owIllegalRegister = owIllegalRegisterMapper.selectById(id);
        OwIllegalRegisterDto dto = mapper.map(owIllegalRegister,OwIllegalRegisterDto.class);
        result.setResultObj(dto);

        return result.success();
    }


    /**
        返回ID值
    */
    @Override
    public Result<OwIllegalRegisterDto> addData(OwIllegalRegisterDto owIllegalRegisterDto){
        Result<OwIllegalRegisterDto> result = new Result();

        //封闭数据
        OwIllegalRegister owIllegalRegister = mapper.map(owIllegalRegisterDto, OwIllegalRegister.class);
        owIllegalRegister.setCreatedate(DateTool.getCurTimestamp());

        //调用mapper
        long i = owIllegalRegisterMapper.insert(owIllegalRegister);
        if(i==0){
            return result.fault("插入失败");
        }

        //插入多张图片
        if(owIllegalRegisterDto!=null && owIllegalRegisterDto.getOwPictureMagDtoList()!=null) {
            for (OwPictureMagDto owPictureMagDto : owIllegalRegisterDto.getOwPictureMagDtoList()) {
                OwPictureMag owPictureMag = mapper.map(owPictureMagDto, OwPictureMag.class);
                //1:front, 2:back （默认设1）
                owPictureMag.setFrontBackSort(
                        StringUtils.isNull(owPictureMagDto.getFrontBackSort())?1:Integer.parseInt(owPictureMagDto.getFrontBackSort()));
                owPictureMag.setOwTableType("ow_illegal_register_id");
                owPictureMag.setOwTableId(owIllegalRegister.getId());
                owPictureMag.setCompanyId(0);
                owPictureMag.setCreatemanId(0);
                owPictureMag.setModifymanId(0);
                owPictureMag.setCreatedate(DateTool.getCurTimestamp());
                owPictureMagMapper.insert(owPictureMag);
            }
        }

        OwIllegalRegisterDto dto = mapper.map(owIllegalRegister,OwIllegalRegisterDto.class);
        result.setResultObj(dto);

        //加入执勤记录中
        OwExecuteRecord owExecuteRecord = new OwExecuteRecord();
        owExecuteRecord.setBusinessTableId(owIllegalRegister.getId());
        owExecuteRecord.setBusinessTableName("ow_illegal_register");
        owExecuteRecord.setCreateman(owIllegalRegister.getCreateman());
        owExecuteRecord.setCreatedate(owIllegalRegister.getCreatedate());
        owExecuteRecord.setCompanyId(owIllegalRegister.getCompanyId());
        owExecuteRecord.setCreatemanId(owIllegalRegister.getCreatemanId());
        owExecuteRecord.setModifymanId(owIllegalRegister.getModifymanId());
        owExecuteRecordMapper.insert(owExecuteRecord);

        return result.success();
    }

    /**
        返回ID值
    */
    @Override
    public Result<OwIllegalRegisterDto> addEditData(OwIllegalRegisterDto owIllegalRegisterDto){
        OwIllegalRegister owIllegalRegister = new OwIllegalRegister();
        owIllegalRegister.setIllegalNumber(owIllegalRegisterDto.getIllegalNumber());
        Map<String,Object> mapParam = new HashMap<>();
        mapParam.put("bean",owIllegalRegister);
        List<OwIllegalRegister> owIllegalRegisterList = owIllegalRegisterMapper.selectByPagination(mapParam);
        if(owIllegalRegisterList==null || owIllegalRegisterList.size()==0){
            //添加数据
            return addData(owIllegalRegisterDto);
        }else{
            //修改数据
            OwIllegalRegister owIllegalRegister1 = owIllegalRegisterList.get(0);
            owIllegalRegisterDto.setId(""+owIllegalRegister1.getId());
            return editData(owIllegalRegisterDto);
        }
    }

    /**
        返回ID值
    */
    @Override
    public Result<OwIllegalRegisterDto> editData(OwIllegalRegisterDto owIllegalRegisterDto){
        Result<OwIllegalRegisterDto> result = new Result();

        //封装数据
        OwIllegalRegister owIllegalRegister = mapper.map(owIllegalRegisterDto, OwIllegalRegister.class);
        owIllegalRegister.setModifydate(DateTool.getCurTimestamp());

        //调用mapper
        long i = owIllegalRegisterMapper.updateById(owIllegalRegister);
        if(i==0){
            return result.fault("编辑数据失败");
        }

        //插入多张图片
        if(owIllegalRegisterDto!=null && owIllegalRegisterDto.getOwPictureMagDtoList()!=null) {
            //有新图片时，把老图片删除了；没有新图片的话，先别删除
            if(owIllegalRegisterDto.getOwPictureMagDtoList().size()>0){
                List<OwPictureMag> owPictureMagList = owPictureMagMapper.selectPictureByOwIllegalRegisterId(owIllegalRegister.getId());
                Map<String,Object> delMap = new HashMap<>();
                delMap.put("modifyman","system");
                delMap.put("modifydate",DateTool.getCurTimestamp());
                for(OwPictureMag owPictureMag : owPictureMagList) {
                    delMap.put("id",owPictureMag.getId());
                    owPictureMagMapper.deleteById(delMap);
                }
            }
            for (OwPictureMagDto owPictureMagDto : owIllegalRegisterDto.getOwPictureMagDtoList()) {
                OwPictureMag owPictureMag = mapper.map(owPictureMagDto, OwPictureMag.class);
                //1:front, 2:back （默认设1）
                owPictureMag.setFrontBackSort(
                        StringUtils.isNull(owPictureMagDto.getFrontBackSort())?1:Integer.parseInt(owPictureMagDto.getFrontBackSort()));
                owPictureMag.setOwTableType("ow_illegal_register_id");
                owPictureMag.setOwTableId(owIllegalRegister.getId());
                owPictureMag.setCompanyId(0);
                owPictureMag.setCreatemanId(0);
                owPictureMag.setModifymanId(0);
                owPictureMag.setCreatedate(DateTool.getCurTimestamp());
                owPictureMagMapper.insert(owPictureMag);
            }
        }


        OwIllegalRegisterDto dto = mapper.map(owIllegalRegister, OwIllegalRegisterDto.class);
        result.setResultObj(dto);

        return result.success();
    }

    /**
    伪删除记录，返回ID值
    */
    @Override
    public Result deleteById(long id,String adminMan){
        Result result = new Result();

        Map<String,Object> mapParameter = new HashMap();
        mapParameter.put("id", id);
        mapParameter.put("modifyman",adminMan);
        mapParameter.put("modifydate",DateTool.getCurTimestamp());

        //调用mapper
        long i = owIllegalRegisterMapper.deleteById(mapParameter);
        if(i==0){
            return result.fault("删除数据失败");
        }

        return result.success();
    }



    /**
     *页面上查询列的值，动态加载，sql语句读数据库配置而来
     */
    public java.util.List select4a2userId(){
        return owIllegalRegisterMapper.select4a2userId();
    }


}
